<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Util\Common;
use App\Http\Util\Err;
use App\Http\Util\HomeTool;
use App\Models\Banner;
use App\Models\Team;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class BannerController extends Controller
{

    /**获取列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {

        $page = request()->only(['page', 'limit', 'type']);
        $list = Banner::where(function ($query) use ($page) {
            if (array_key_exists('type', $page) && $page['type']) {
                $query->where('type',$page['type'] );
            }
            $query->where('is_del', Err::DEL_SHOW);
        })->orderBy('sort', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get(['id','sort','thumb','url','type','created_at','addr'])->toArray();
        $count = Banner::where(function ($query) use ($page) {
            if (array_key_exists('type', $page) && $page['type']) {
                $query->where('type',$page['type'] );
            }
            $query->where('is_del', Err::DEL_SHOW);
        })->count();
        return response()->json(['data' => $list, 'total' => $count, 'code' => 200]);
    }

    /**创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $data = $request->post();
        if (array_key_exists('id', $data) && $data['id'] > 0) {
            $check_roles = HomeTool::checkRoles(45,'edit');
            if(!$check_roles){
                return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
            }
            $id = $data['id'];
            unset($data['id']);
            $model=Banner::where('id', $id)->update($data);
        } else {
            $check_roles = HomeTool::checkRoles(45,'add');
            if(!$check_roles){
                return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
            }
            $model=Banner::create($data);
        }
        if(Redis::exists(Common::SLICK_IMAGE.$data['type'])){
            Redis::del(Common::SLICK_IMAGE.$data['type']);
        }
        return response()->json(['code' => 200, 'msg' => '成功']);
    }


    /**删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        $check_roles = HomeTool::checkRoles(45,'del');
        if(!$check_roles){
            return response()->json(['code' => 1001, 'msg' => '暂无此操作权限']);
        }
        $id = $request->get('id');
        if (!$id) {
            return response()->json(['code' => 1001, 'msg' => '无此条信息']);
        }
        $model = Banner::find($id);
        $model->is_del = Err::DEL_HIDE;
        $model->save();
        if(Redis::exists(Common::SLICK_IMAGE.$model->type)){
            Redis::del(Common::SLICK_IMAGE.$model->type);
        }
        return response()->json(['code' => 200, 'msg' => '删除成功']);
    }

    public function get_back_image(){
        $url = Redis::get('BACK_IMAGE_URL');
        return response()->json(['code' => 200, 'msg' => '成功','url'=>$url]);
    }
}
